# 记录歼灭敌机的数量
import pygame

from eventType import *
from bg_color import *

class AchieveMent:
    def __init__(self, screen, screen_rect):
        self.screen = screen
        self.screen_rect = screen_rect
        # 记录歼敌数量
        self.achievement = {}

        self.startY = 1

    def update(self, enemy_filename):
        if self.achievement.get(enemy_filename) is None:
            image = pygame.image.load(enemy_filename)
            image = pygame.transform.scale(image, (40, 40))
            image = pygame.transform.rotate(image, 90)
            rect = image.get_rect()
            self.achievement[enemy_filename] = {
                "count": 0,
                "image": image,
                "rect": rect,
                "top_p": self.startY
            }
            self.startY += 1

        self.achievement[enemy_filename]['count'] += 1

        # 判断歼灭的敌机数量
        if enemy_filename == 'images/enemy1.png' and self.achievement[enemy_filename]['count'] % 2 == 0:
            # 每歼灭4架小飞机，则创建一家打飞机
            event_queue = pygame.event.Event(EVENT_ADD_HUGE_ENEMY)
            pygame.event.post(event_queue)
        
        self.display()
    
    def display(self):
        for key in self.achievement.keys():
            self.achievement[key]['rect'].x = self.screen_rect.width - 180
            self.achievement[key]['rect'].y = 55 * self.achievement[key]['top_p']
            self.screen.blit(self.achievement[key]['image'], self.achievement[key]['rect'])
            countTxt = pygame.font.SysFont('comicsans', 20, True).render('+' + str(self.achievement[key]['count']), True, bg_red)

            self.screen.blit(countTxt, (self.achievement[key]['rect'].x + self.achievement[key]['rect'].width + 10, self.achievement[key]['rect'].y))